<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>

<html>
<head>
    <s:set name="isNew" value="id == null"/>
    <s:set var="title">
        <s:if test="#isNew">创建</s:if><s:else>修改</s:else>用户
    </s:set>

    <title><s:property value="#title"/></title>
    <script type="text/javascript" src="${ctx}/js/jstree/jquery.jstree.js"></script>
    <script type="text/javascript" src="${ctx}/js/groupTree.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            //聚焦第一个输入框
            $("#loginName").focus();
            //为inputForm注册validate函数
            $("#inputForm").validate({
                                         rules: {
                                             userId: {
                                                 required: true,
                                                 remote: "user!checkLoginName.action?oldUserId="
                                                         + encodeURIComponent('${userId}')
                                             },
                                             firstName: "required",
                                             deptId: "required",
                                             password: {
                                                 required: true,
                                                 minlength:3
                                             },
                                             passwordConfirm: {
                                                 equalTo:"#password"
                                             },
                                             email:"email",
                                             checkedRoleIds:"required"
                                         },
                                         messages: {
                                             userId: {
                                                 remote: "用户登录名已存在"
                                             },
                                             passwordConfirm: {
                                                 equalTo: "输入与上面相同的密码"
                                             }
                                         }
                                     });
              
            initDeptTree(); // add by sevenSpend 2011/11/27
            bindTreeSelectedFUN();
        });

        function submitForm() {
            getCheckList();
            $('#inputForm').submit()
        }
        // add by sevenSpend 2011/11/27 -->
        function initDeptTree() {

            var checkedDeptIds = $("#checkedDeptIdsStr").val().split(",");

            var updateURL = "/security/user!input.action";
            var deptTree = initGroupTreeCheckbox(${deptJson}, '${ctx}', updateURL, 'treeDept');
             var deptId = $("#deptId").val();

            deptTree.bind("loaded.jstree", function (event, data) {
            	$("#treeDept").jstree("open_all");

				$("#treeDept").find("li").each(function() {
					for (var i = 0; i < checkedDeptIds.length; i++) {
						if ($(this).attr("id") == checkedDeptIds[i]) {
							$("#treeDept").jstree("check_node", $(this));
							break;
						}
					}

                    if(deptId && $(this).attr("id")==deptId ) {
                        $("#treeDept").jstree("select_node", $(this));
                    }
				});
            });
        }
        
        function getCheckList(obj) {
			var ids = getCheckId(obj);
			var strIds = "";
			for (var i=0; i<ids.length; i++) {
				strIds += ids[i];
				strIds += ",";
			}

			if (strIds == "") {
				$("#checkedDeptIdsStr").val("");
				return;
			}
			
			$("#checkedDeptIdsStr").val(strIds.substr(0, strIds.length-1));
		}

        function getCheckId(obj) {
			var ids =[];
			$.jstree._reference($("#treeDept")).get_checked(obj).each(function(i, n) {
				ids.push(n.id);
				if ($(this).find("> ul").length > 0) {
					ids.push(getCheckId($(this)));
				}
			});
			return ids;
		}
        // add by sevenSpend 2011/11/27 <--

        function bindTreeSelectedFUN(obj){
            $("#treeDept").bind("select_node.jstree",function (event, data) {
                $("#selectedDept").val(data.rslt.obj.attr("name"));
                $("#deptId").val(data.rslt.obj.attr("id"));
            } )
        }
    </script>
    <meta name="menu" content="useManage"/>
</head>

<body>

<div class="content_block">
    <h2 class="jquery_tab_title"><s:property value="#title"/></h2>

    <form id="inputForm" action="user!save.action" method="post">
        <input type="hidden" name="revision" value="${revision}"/>
        <input type="hidden" name="id" value="${id}"/>

        <p>
            <label for="userId">登录名: </label>
            <input name="userId" id="userId" type="text" value="${userId}" class="input-small" <s:if test="!#isNew">readonly="true"</s:if> >
        </p>

        <p>
            <label for="password">密码: </label>
            <input name="password" id="password" type="password" value="${password}" class="input-small"/>
        </p>

        <p>
            <label for="passwordConfirm">确认密码: </label>
            <input name="passwordConfirm" id="passwordConfirm" type="password" value="${password}" class="input-small"/>
        </p>

        <p>
            <label for="firstName">姓名: </label>
            <input name="firstName" id="firstName" type="text" value="${firstName}" class="input-small"/>
        </p>

        <p>
            <label for="searchName">查找名字: </label>
            <input name="searchName" id="searchName" type="text" value="${searchName}" class="input-small"/>
        </p>

        <p>
            <label for="jobTitle">职位: </label>
            <input name="jobTitle" id="jobTitle" type="text" value="${jobTitle}" class="input-small"/>
        </p>

        <p>
            <label for="telephone">固定电话: </label>
            <input name="telephone" id="telephone" type="text" value="${telephone}" class="input-small"/>
        </p>

        <p>
            <label for="ext">短 号: </label>
            <input name="ext" id="ext" type="text" value="${ext}" class="input-small"/>
        </p>

        <p>
            <label for="cellPhone">手  机: </label>
            <input name="cellPhone" id="cellPhone" type="text" value="${cellPhone}" class="input-small"/>
        </p>


        <p>
            <label for="enabled">状态: </label>
            <code:dispCodeList id="enabled" name="enabled" codeType="USER_ENABLED"  codeValue="${enabled}"/>
        </p>

        <p>
            <label>所属部门:</label>
            <input type="text" id="selectedDept" readonly="true" value='<code:dispCodeDesc table="ACT_ID_GROUP" codeColumn="NAME_"  valueColumn="ID" codeValue="${deptId}" />' />
            <input type="hidden" id="deptId"  name="deptId" value="${deptId}" />
       </p>
        <p>
            <!-- update by sevenSpend 2011/11/24 start -->
            <label>管理部门:</label>
             <div id="treeDept" style="width: 400px"></div>
            <input type="hidden" id="checkedDeptIdsStr" name="checkedDeptIdsStr" value="${checkedDeptIdsStr}" />
            <!-- update by sevenSpend 2011/11/24 end -->
        </p>

        <p>
            <label>角色: </label>
            <s:checkboxlist name="checkedRoleIds" list="allRoleList" listKey="id" listValue="name"
                            theme="custom"/>
        </p>

        <p>
            <label for="address">地址:</label>
            <textarea name="address" id="address" cols="60" rows="10">${address}</textarea>
        </p>

        <p>
            <a class="button" href="javascript:submitForm();" onclick="javascript:getCheckList()"><span>提交</span></a>
            <a class="button" onclick="javascript:window.location.href='user.action'"><span>返回</span></a>
        </p>
    </form>
</div>
</body>
</html>
